<template>
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="1000px"
    center
  >
    <com-table v-if="listType === '2'" :data="dataList" :columns="columns" :total="total" @pageChange="pageChange">
      <template v-slot:type="{row: {type}}">
        {{ type ? docTypes[type - 1] : '' }}
      </template>
      <template v-slot:name="{row}">
        <a class="com-link" @click="viewDetail('2', row)">{{ row.name }}</a>
      </template>
      <template v-slot:status="{row: {status}}">
        {{ status === 1 ? '未归档' : '已归档' }}
      </template>
      <template v-slot:createTime="{row: {createTime}}">
        {{ createTime | date }}
      </template>
    </com-table>
    <com-table v-if="listType === '1'" :data="dataList" :columns="columns" :total="total" @pageChange="pageChange" />
  </el-dialog>
</template>

<script>
import { getDocumentDetailList } from '@/api/knowledgeBase'
import { getUpcomingWorkflowTasks } from '@/api/flow'
export default {
  name: 'IndexModalList',
  components: {

  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    listType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      visible: false,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      docTypes: ['文档', '原件', '复印件', '扫描件']

    }
  },
  computed: {
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    },
    title() {
      const titles = {
        '1': '待办任务',
        '2': '知识库'
      }
      return titles[this.listType]
    },
    columns() {
      const columnsEnum = {
        '1': [
          {
            label: '所属菜单',
            prop: 'menu'
          },
          {
            label: '标题',
            prop: 'title'
          },
          {
            label: '编制时间',
            prop: 'createTime',
            slot: 'createTime',
            width: '90'
          }
        ],
        '2': [
          {
            label: '编号',
            prop: 'number'
          },
          {
            label: '所属类别',
            prop: 'keyword'
          },
          {
            label: '标题',
            slot: 'name',
            attrs: {
              showOverflowTooltip: true
            }
          },
          {
            label: '文档类型',
            prop: 'type',
            slot: 'type'
          },
          {
            label: '文档状态',
            prop: 'status',
            slot: 'status'
          }
        ]
      }
      return columnsEnum[this.listType]
    }
  },
  watch: {
    showModal: {
      handler(newData) {
        if (newData) {
          this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    pageChange(data) {
      this.pageParams = data
      this.getList()
    },
    getList() {
      const fns = {
        '1': getUpcomingWorkflowTasks,
        '2': getDocumentDetailList
      }
      const params = {
        ...this.pageParams
      }
      const fn = fns[this.listType]
      fn(params).then(res => {
        if (res && res.code === '0001') {
          if (this.listType === '2') {
            this.dataList = res.data.data
          } else {
            this.dataList = res.data
          }
        }
      })
    },
    viewDetail(type, row) {
      this.$emit('viewDetail', type, row)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
